//
//  LMJRedioController.m
//  EPA
//
//  Created by lanou3g on 16/4/20.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "LMJRedioController.h"
#import "LMJRedioListModel.h"
#import "LMJRedioCell.h"
#import "LMJNetWorkRequestManager.h"
#import "URLHeaderDefine.h"
#import "LMJRadioPlayController.h"
#import "LORefresh.h"
@interface LMJRedioController ()
//数组
@property (nonatomic, strong)NSMutableArray *redioArray;

@property (nonatomic,strong)NSString *start;
@property (nonatomic,strong) LORefresh *headerRefresh;
@end

@implementation LMJRedioController
-(void)dealloc {
    //下拉
    [self.tableView removeObserver:[self.tableView gifHeader] forKeyPath:@"contentOffset"];
    //footer
    [self.tableView removeObserver:[self.tableView defaultFooter] forKeyPath:@"contentOffset"];
    [self.tableView removeObserver:[self.tableView defaultFooter] forKeyPath:@"contentSize"];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"电台详情";
    self.redioArray = [NSMutableArray new];
    //上拉加载,下拉刷新
    //请求数据
    self.start = @"0";
    [self loadDataFromServerType:self.type Start:self.start];
    //注册重用标识
    [self.tableView registerNib:[UINib nibWithNibName:@"LMJRedioCell" bundle:nil] forCellReuseIdentifier:@"redioCell"];
#pragma mark - radio上下拉刷新
    //上下拉刷新
    __weak typeof(self) weakSelf = self;
    [self.tableView addRefreshWithRefreshViewType:LORefreshViewTypeHeaderGif refreshingBlock:^{
        //移除
        [weakSelf.redioArray removeAllObjects];
        [weakSelf.tableView reloadData];
        //网络请示
        [weakSelf loadDataFromServerType:weakSelf.type Start:@"0"];
    }];
    //gif图刷新
    self.headerRefresh = [self.tableView gifHeader];
    [self.headerRefresh setGifName:@"wangnima"];
    //上提
    [self.tableView addRefreshWithRefreshViewType:LORefreshViewTypeFooterDefault refreshingBlock:^{
        NSInteger count = weakSelf.redioArray.count;
        weakSelf.start = [NSString stringWithFormat:@"%ld",count];
        //网络请求
        [weakSelf loadDataFromServerType:weakSelf.type Start:weakSelf.start];
    }];
    

}
//上拉加载,下拉刷新调用方法
-(void) loadDataFromServerType:(NSString *)type Start:(NSString *)start {
    //网络请求
    [LMJNetWorkRequestManager requestURL:[NSURL URLWithString:RADIODETAILLIST_URL] Method:LMJ_POST Params:@{@"auth":@"",@"client":@"1",@"deviceid":@"63A94D37-33F9-40FF-9EBB-481182338873",@"radioid":self.RadioID,@"version":@"3.0.4"} Result:^(NSData *data, NSURLResponse *response, NSError *error) {
        //解析数据
        NSMutableDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
//        NSLog(@"=====%@",jsonDic);
        NSDictionary *dataDic = jsonDic[@"data"];
//        NSLog(@"=====%@",dataDic);
        for (NSDictionary *itemDic  in dataDic[@"list"]) {
            LMJRedioListModel *rModel = [LMJRedioListModel new];
            [rModel setValuesForKeysWithDictionary:itemDic];
            [self.redioArray addObject:rModel];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            //刷新数据
            [self.tableView reloadData];
            //手动调用停止刷新,加载
            [[self.tableView gifHeader]endRefreshing];
            [[self.tableView defaultFooter]endRefreshing];
        });
    }];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    return self.redioArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LMJRedioCell *cell = [tableView dequeueReusableCellWithIdentifier:@"redioCell" forIndexPath:indexPath];
    
    // Configure the cell...
    cell.model = self.redioArray[indexPath.row];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 120;
}
//跳转
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    LMJRadioPlayController *playVC = [[LMJRadioPlayController alloc]init];
    //传值
    playVC.radioArray =self.redioArray;
    playVC.currentIndex= indexPath.row;
    [self.navigationController pushViewController:playVC animated:YES];
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
